package jeecg.business.thread.task.impl;

import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.log.CollLogEntity;
import jeecg.business.entity.site.SimpleSiteEntity;
import jeecg.business.entity.site.SiteEntity;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.clean.HtmlCleanBaseUtil;
import org.jeecgframework.core.util.clean.ListHtmlCleanUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;
import org.jeecgframework.core.util.http.HttpClientUtil;
/**
 * 
 * <p>
 * 标题: 百度搜索
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since 2015-6-7 上午9:15:26
 */
public class BaiduYinqingExecTask extends CommonExecTask{

	private static final String String = null;

	public BaiduYinqingExecTask(EnterEntity enter , KeywordEntity keyword, SiteEntity site) {
		super(enter , keyword, site);
	}

	@Override
	public void run() {
		CollLogEntity log = null;
		log = saveLog();
		try{
			//{longstarttime}
			System.out.println("exec task......");
			String basehref = site.getZddz();
			String word = URLEncoder.encode(keyword.getKeyword(),site.getWyzfj());
			basehref = basehref.replace(KEYWORD_FLAG, word);
			//在设置时间，近一个月 {starttime} {endtime}
			basehref = basehref.replace(STARTTIME_FLAG, getStartTime());
			basehref = basehref.replace(ENDTIME_FLAG, getEndTime());
			setCookies();
			do{
				current++;
				System.out.println(keyword.getKeyword()+"-----"+site.getMc()+"-----第"+current+"页"+"-----"+basehref);
				if(StringUtil.isNotEmpty(basehref)){
					List<HtmlParseResult> infolist = ListHtmlCleanUtil.listInfo(basehref, site, keyword.getKeyword());
					String contenthref = null;
					if(infolist != null && infolist.size()>0){
						KeyinfoEntity keyinfo = null;
						for(HtmlParseResult result : infolist){
							contenthref = result.getUrl();
							//重定向
							contenthref = HttpClientUtil.getRediectUrl(contenthref, site.getWyzfj(), cookies);
							result.setUrl(contenthref);
							System.out.println("抓取-------"+contenthref);
//							if(contenthref.contains("news.fznews.com.cn"))
//								System.out.println("mc");
							
							if(keyinfoService.hasHref(contenthref , enter.getId())) continue;
							try{
								/**
								 * 设置内容
								 */
								String roothref = StringUtil.getRootHref(contenthref);
								SimpleSiteEntity simpleSite = simpleSiteService.query(roothref);
								
								String bmlx = "utf-8";
								
								if(simpleSite == null){		//添加到数据库中。
									simpleSite = new SimpleSiteEntity();
									simpleSite.setZdbs(roothref);
									simpleSite.setSfpp("2");
									simpleSite.setYldz(contenthref);
									simpleSite.setZnbm("2");
									simpleSite.setWzlx("ssyq");
									bmlx = getEncodeByHref(contenthref, bmlx, cookies);
									simpleSite.setBmlx(bmlx);
									simpleSite.setZwnr("//body");
									simpleSiteService.save(simpleSite);
								}else{
									bmlx = simpleSite.getBmlx();
									if("1".equals(simpleSite.getZnbm()))
										bmlx = getEncodeByHref(contenthref, bmlx, cookies);
								}
								
								
								
								
								TagNode node = null;
								HtmlCleaner cleaner = new HtmlCleaner();
								String html = HttpClientUtil.getSourceFromUrl(contenthref, bmlx, cookies);
								node = cleaner.clean(html);
								String titleXpath = "//title";
								String zwnrXpath = "//body";
								if(StringUtil.isNotEmpty(simpleSite.getZwnr())) zwnrXpath = simpleSite.getZwnr();
								String content = HtmlCleanBaseUtil.getTagContent(node, zwnrXpath);
								String title = HtmlCleanBaseUtil.getTagContent(node, titleXpath);
								/**
								 * 是否是标题去重
								 */
								if("2".equals(simpleSite.getPcfs()) && keyinfoService.hasTitle(title,enter.getId())){
									continue;
								}
								
								result.setTitle(title);
								keyinfo = new KeyinfoEntity(result);
								if(StringUtil.isNotEmpty(content)){
									content = StringUtil.removeHTMLLableExe(content);
									keyinfo.setBodycontent(content);
								}
									
								keyinfo.setQyid(enter.getId());
								System.out.println(result.getUrl()+"----"+result.getTitle()+"----"+(keyinfo.getBodycontent()!=null?keyinfo.getBodycontent().length():0)+"----"+result.getEventDate());
								if(filter(enter, keyinfo , keyword))		//过滤
									keyinfoService.saveKeyinfo(keyinfo);
							}catch(Exception e){
//								e.printStackTrace();
								System.out.println("抓取失败："+contenthref);
							}
						}
						basehref = infolist.get(0).getNextPageUrl();
						basehref = basehref.replace("&amp;", "&");
//						System.out.println("下一个地址："+basehref);
						
						if(basehref!=null && !basehref.startsWith("http"))
							basehref = site.getGdz()+basehref;
					}
				}
			}while(current <= maxpage);
//			log = collLogService.get(CollLogEntity.class, log.getId());
		}catch(Exception e){
//			e.printStackTrace();
//			log.setSfcg("2");
//			log.setCwxx(e.getMessage());
		}finally{
			log = collLogService.get(CollLogEntity.class, log.getId());
			log.setJssj(new Date());
			collLogService.updateEntitie(log);
			System.out.println("end....");
		}			
	}

}
